package com.luchao.test;

import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 解析获取JSON数据
 */
public class FastJSONDemo {
	
	public static void main(String[] args) {
//		String string = "{\"info\":{\"status\":\"SUCCESS\"},\"data\":{\"isOaUser\":true,\"gender\":\"MALE\",\"phone\":\"\",\"icon\":\"http://apptest.sggf.com.cn:8801/defaultroot/upload/peopleinfo/2017071210005832078600687.jpg\",\"name\":\"赵路\",\"duty\":\"员工\",\"employeeId\":\"126490\",\"departmentHierarchy\":\"首钢集团/OA运维项目组\",\"job\":\"员工\",\"cellPhone\":\"\",\"username\":\"zhaol3317\"}}";
//		String string = "{\"info\":{\"status\":\"SUCCESS\"},\"data\":{\"isOaUser\":true,\"gender\":\"MALE\"}}";
		String string = "{\"eqCheckAMbook\":\"书名\",\"eqworkshop\":\"车间\",\"eqregionalSupervisor\":\"主管\",\"eqpreparer\":\"填表人\",\"eqNowDate\":\"日期\",\"eqLastDate\":\"上次日期\",\"tableArr\":[{\"positionName\":\"部位名称\",\"imgsrc\":\"E:\\\\jboss-eap-6.4\\\\standalone\\\\deployments\\\\Cloud.ear\\\\CloudWeb.war\\\\files\\\\20190224114332.jpg\",\"subArray\":[{\"positionName\":\"部位名称\",\"category\":\"类别\",\"type\":\"R\",\"position\":\"部位\",\"baseName\":\"基准\",\"method\":\"方法\",\"tool\":\"用眼睛看\",\"time\":\"时间\",\"day\":\"天\",\"week\":\"周\",\"headName\":\"责任人\",\"imgsrc\":\"E:\\\\jboss-eap-6.4\\\\standalone\\\\deployments\\\\Cloud.ear\\\\CloudWeb.war\\\\files\\\\20190224114332.jpg\",\"index\":\"1\"}]},{\"imgsrc\":\"E:\\\\jboss-eap-6.4\\\\standalone\\\\deployments\\\\Cloud.ear\\\\CloudWeb.war\\\\files\\\\20190224114341.jpg\",\"positionName\":\"部位\",\"subArray\":[{\"positionName\":\"部位\",\"category\":\"2\",\"type\":\"R\",\"position\":\"2\",\"baseName\":\"2\",\"method\":\"2\",\"tool\":\"用眼睛看\",\"time\":\"2\",\"day\":\"2\",\"week\":\"2\",\"headName\":\"2\",\"imgsrc\":\"E:\\\\jboss-eap-6.4\\\\standalone\\\\deployments\\\\Cloud.ear\\\\CloudWeb.war\\\\files\\\\20190224114341.jpg\",\"index\":\"2\"}]}]}";
		 
//		JSONObject hostObject = JSONObject.parseObject(string);
//		Map<String, String> map =(Map<String, String>) hostObject.get("info");
//		Map<String, String> map2 =(Map<String, String>)hostObject.get("data");
//		System.out.println(map2.get("username"));
//
//		String string2 = "{\"code\":403,\"msg\":\"用户已在其他设备上登录，请重新登录\",\"token\":1512550962844}";
//		JSONObject jObject = JSONObject.parseObject(string2);
//		Object ob = jObject.get("code");
//		if(null!=ob){
//			System.out.println(ob.toString());
//		}

		JSONObject jsonObject1 = JSONObject.parseObject(string);
		//获取最外层数据
		System.out.println("eqCheckAMbook:"+jsonObject1.get("eqCheckAMbook"));
		System.out.println("eqworkshop:"+jsonObject1.get("eqworkshop"));
		//获取tableArr数据,因为是数据，所以使用JSONArray
		JSONArray jsonArray =jsonObject1.getJSONArray("tableArr");
		for (int i =0;i<jsonArray.size();i++){
			JSONObject jobt = (JSONObject) jsonArray.get(i);
			System.out.println("数组"+i+"的positionName字段值:"+jobt.get("positionName"));
			//数组中的subArray数组
			JSONArray jsonArray1 = jobt.getJSONArray("subArray");
			for (int j = 0;j<jsonArray1.size();j++){
                JSONObject jjj = (JSONObject) jsonArray1.get(j);
                //System.out.println("subArray数组"+j+":"+jjj.toString());
                System.out.println("subArray数组"+j+":"+jjj.get("method"));
                System.out.println("subArray数组"+j+":"+jjj.get("type"));
                System.out.println("subArray数组"+j+":"+jjj.get("tool"));
            }
		}

	}

}
